﻿<?if(!defined('sugarEntry'))define('sugarEntry', true);
/*********************************************************************************
module : vng sales marketing
author : hoanghd@vinagame.com.vn
********************************************************************************/
require_once('XTemplate/xtpl.php');
require_once('log4php/LoggerManager.php');
require_once('include/modules.php');
require_once('modules/ZT2Remit/ZT2Remit.php');

global $current_language;
global $app_strings;
global $app_list_strings;
global $theme;
$theme_path = 'themes/' . $theme . '/';
$image_path = $theme_path .'images/';
require_once($theme_path . 'layout_utils.php');
global $mod_strings;



$mode_inpvl=$_POST["mode_inpvl"];
$insert=$_GET["insert"];
$seed = new ZT2Remit_Ext();
$fnc = new FNC();


$pst_mode=$_POST["mode"];
$pst_chk=$_POST["chk"];

//print "<pre>";
//print_r($_SESSION);

// kiem tra quyen access 
if ($seed->ACLAccess('view') && $seed->ACL_Access('List')) {
	ob_start();





	$list_form = new XTemplate('modules/ZT2Remit/List.html');
	$seed->set_theme($list_form);
	$PN_Error = '';

	$search_type[0]='Tên chương trình';
	$search_field[0]='configName';


	$_where="";
	$_vl_search_type=$_POST["search_type"];
	$_vl_keyword=$_POST["keyword"];
	$_vl_from_date=$_POST["from_date"];


	if ($_vl_keyword!='' && $_vl_search_type!=''){
			$_where.=" and ".$search_field[$_vl_search_type]." like ''%".$seed->anti($_vl_keyword)."%'' ";
	}
	
	$_vl_gamecode_list=$_POST["gamecode_list"];
	if ($_vl_gamecode_list!=''){
		$_where.=" and gamecode = ''".$seed->anti($_vl_gamecode_list)."'' ";	
	}

	$gridview='';
	$DBList = $seed->setList($fnc,$_where,$_vl_from_date,$list_form);	
	$gridview.= $seed->_AddPageHeader();
	$gridview.= $seed->PageHeader();
	$exportGrid = $seed->setHeader();
	$exportGrid.= $DBList[0];
	$gridview.= $exportGrid;		
	$gridview.=$seed->setFooter();
	$gridview.=$seed->setPaging();

	
	$title='Danh sách đăng ký';
	if($_POST['mode']=='EXPORT' && $seed->ACL_Page(1,'List'))
		{			
			$seed->ExportToExcel($seed->fnStrConvert($title),$exportGrid);							
			die();
		}







	$arr_info_gamecode_list = $seed->fnc_get_gamecode();        
	//print_r($arr_info_gamecode_list);
	$arr_attr_gamecode_list = array('style'=>'');
	$sel_info_gamecode_list = $fnc->addSelectList2('gamecode_list',$arr_info_gamecode_list,'--- Tất cả ---',$arr_attr_gamecode_list,$list_form,$_gamecode_list); 




	$attr_search_type = array("style"=>'','title'=>'Tìm kiếm theo');
	$search_type = $fnc->addSelectList('search_type',$search_type,'--- Tìm kiếm theo ---',$attr_search_type,$list_form);	


	$arr_attr_keyword = array('style'=>'');
	$inp_keyword = $fnc->addInput('text','keyword',$keyword,$arr_attr_keyword,$list_form);

	$arr_attr_from_date = array('style'=>'');
	$inp_from_date = $fnc->addInput('text','from_date',$from_date,$arr_attr_from_date,$list_form);
	
	if ($seed->ACL_Page(1,'List')){	
		$arr_attr_btnexport = array('style'=>'','onclick'=>'sbm_form(2)');
		$inp_btnexport = $fnc->addInput2('button','btnexport',$btnexport,$arr_attr_btnexport,$list_form,'Xuất Excel'); 
	}

	if ($seed->ACL_Page(2,'List')){	
		$arr_attr_btndelete = array('style'=>'','onclick'=>'sbm_form(3)');
		$inp_btndelete = $fnc->addInput2('button','btndelete',$btndelete,$arr_attr_btndelete,$list_form,'Xóa'); 
	}

	/*----------------------*/
	$list_form->assign('tabs'	,  	$seed->set_tabs());
	/*----------------------*/	
	$list_form->assign('MODULE'	,  	$seed->object_name);
	$list_form->assign('PN_Error',	$PN_Error);
	$list_form->assign('P_INFO',	$P_INFO);
	$list_form->assign('gridview'	,  	$gridview);
	$list_form->assign('title'	,  	$title);
	$list_form->assign('page_name'	,  	$sugar_config['submenu_name'][$seed->object_name]);

	$list_form->parse('main');
	$list_form->out('main');
	
	ob_end_flush();
} else {
	ACLController::displayNoAccess();
	sugar_die('');
}

class ZT2Remit_Ext  extends ZT2Remit
{
 
	//======================//
	function _AddPageHeader(){
		$_PageHeader=' <br>';
		/*if ($this->ACL_Access('Create','ePMTT')){		
			$_PageHeader=' <br><input type=button value="Thêm mới bạn bè" onclick="user_add()"> <br> <br>'; 
		}*/
		return 	$_PageHeader;	
	}



	function setHeader(){		

		if ($_POST['mode']=='EXPORT' || !$this->ACL_Page(2,'List')){
			$ArrHeader = array('STT'						
			,'Game'
			,'Tên chương trình'
			
			,'Ngày chạy CT'
			,'Ngày tạo TK '				
			,'SL Code Default'
			,'Max User'
			,'Trạng thái'	
			);
		}
		else{
			$ArrHeader = array('<input type=checkbox id=check_all name=check_all>'						
			,'Game'
			,'Tên chương trình'
			
			,'Ngày chạy CT'
			,'Ngày tạo TK '				
			,'SL Code Default'
			,'Max User'
			,'Trạng thái'				
			);
			
		}
		$SortHeader = array(0,1,1,1,1,0,0,0);
		//$SortImage[$_POST['_SortOrderBy']]='css_right ui-icon ui-icon-carat-2-n-s';
		if ($_POST['_OrderDirection']=='desc'){
			$SortImage[$_POST['_SortOrderBy']]='css_right ui-icon ui-icon-triangle-1-s';
		}else if ($_POST['_OrderDirection']=='asc'){
			$SortImage[$_POST['_SortOrderBy']]='css_right ui-icon ui-icon-triangle-1-n';
		}
		
		
		$_Header='';
		$_Header.='<div class="hastable"><div id="aj_result"><table   width="100%"  cellspacing="0"  cellpadding="0" border=0>';
		$_Header.='<thead><tr>';
		
		for ($j=0;$j<count($ArrHeader);$j++)
		{
		
			if ($SortHeader[$j])
			{
				if ($SortImage[$j]==''){$SortImage[$j]='css_right ui-icon ui-icon-carat-2-n-s';}
				
				$_Header.='<th class="ui-state-default t_cap" valign=top>
				<a href=javascript:fnOrder('.$j.') >'.$ArrHeader[$j].'
					<span class="'.$SortImage[$j].'"></span>
				</a></th>';
			}
			else
				$_Header.='<th class="ui-state-default t_cap">'.$ArrHeader[$j].'</th>';
		}
		$_Header.='</tr></thead>';
		return $_Header;
	}
	
	function setList($fnc,$_Where,$_vl_from_date,$list_form){
		$result = NULL;	
		$PageSize=10;		
		$this->PageIndex = isset($_POST["page"]) ? $_POST["page"] : 1;		
		if ($_POST['ddlPageSize']!=''){$PageSize=$_POST['ddlPageSize'];}
		$this->PageSize = $PageSize;
		$_FlgExport=false;
		if($_POST['mode']=='EXPORT'){$PageSize=0;$_FlgExport=true;}

		$_Table = '';
		if ($_FlgExport)
		{	$_Field = array('configName','gamecode','from_date','to_date');	
		}else{
			$_Field = array('configName','gamecode','from_date','to_date');	
		}
		
		$_strField = implode(',', $_Field);
		if ($filter!='')	
		{
			$filter_arr = explode('|',$filter);
		}
			
	
		// ===== order =============/
		$_SortOrderBy=$_POST["_SortOrderBy"];
		$_OrderDirection=$_POST["_OrderDirection"];
		
		if (trim($_SortOrderBy)=='' || $_FlgExport)
		{
			$_OrderBy = 'regDate';
			$_OrderDirection = 'desc';	
		}
		else
		{
			$_OrderBy = $_Field[$_SortOrderBy-1];			
		}
		// ===== order =============/
		
		$_Where.='';
		if (trim($_vl_from_date)!=""){
			$_Where.=" and datediff(d,from_date,''".$this->anti($fnc->vndate2date($_vl_from_date),4)."'')=0 ";		
		}

		try
		{	

	
			$_dblist='';			
			$i=0;
			
			
			if ($_POST['mode_inpvl']=='EXPORT')			
				$sSQL="usp_ZT2_Remit_be_GRLV_Members_List 1,0,' and 1=1 $_Where ','".$_OrderBy."','".$_OrderDirection."','*'";			
			else
				$sSQL="usp_ZT2_Remit_be_GRLV_Members_List ".$this->PageIndex.",".$PageSize.",' and 1=1 $_Where  ','".$_OrderBy."','".$_OrderDirection."','*'";			
			////
            echo $sSQL;
            
			$this->ws_debug($sSQL);
				
				$result = $this->db->query($sSQL, true, "Query failed");					
				
				
			
				
				while ($aR = $this->db->fetchByAssoc($result))
				{
					
					if ($i==0){
						$this->TotalRecord = $fnc->isNull($aR['TotalRecord'],0);						
						$this->TotalPage = intval($this->TotalRecord/$PageSize + ($this->TotalRecord%$PageSize > 0?1:0));					
										
					}					
				
					$STT = ($i+($this->PageIndex-1)*$PageSize)+1;	
					$Id = $aR['configId'];		
					
					$_vconfigId = $aR['configId']; 
					$_vconfigName = $aR['configName']; 
					$_vgamecode = $aR['gamecode']; 
					$_vfrom_date = $aR['from_date']; 
					$_vto_date = $aR['to_date']; 
					$_vstatusId = $aR['statusId']; 
					$_vcreate_date = $aR['create_date']; 
					$_vcreate_by = $aR['create_by']; 
					$_vmodify_date = $aR['modify_date']; 
					$_vmodify_by = $aR['modify_by']; 
					$_vmaxUser = $aR['maxUser']; 
					$_vmaxLevel = $aR['maxLevel']; 
					$_vis_code = $aR['is_code']; 
					$_vis_efriend = $aR['is_efriend']; 
					$_veffect_fromdate = $aR['effect_fromdate']; 
					$_veffect_todate = $aR['effect_todate']; 
					$_vmode_pm = $aR['mode_pm']; 
					$_vmode_gamer = $aR['mode_gamer']; 
					$_vmode_register = $aR['mode_register']; 
					$_vkpi_pm = $aR['kpi_pm']; 
					$_vkpi_gamer = $aR['kpi_gamer']; 
					$_vkpi_revenue = $aR['kpi_revenue']; 
					$_vkpi_playingtime = $aR['kpi_playingtime']; 
					$_vtotal_pm = $aR['total_pm']; 
					$_vtotal_code_default = $aR['total_code_default']; 
					$_vtotal_code_promotion_usr = $aR['total_code_promotion_usr']; 
					$_vtotal_code_promotion_pm = $aR['total_code_promotion_pm']; 
					$_vstatus_code = $aR['status_code']; 
					$_vgamename = $aR['gamename']; 	
					$_vqty_code_default = $aR['qty_code_default']; 
					$_vqty_code_promotion = $aR['qty_code_promotion']; 
					$_vqty_code_promotion_pm = $aR['qty_code_promotion_pm']; 

					$_dblist.='<tr>';
							
							if ($_POST['mode']=='EXPORT' || !$this->ACL_Page(2,'List')){
								$_dblist.='<td  style="text-align:center" width=50>'.$STT.'</td>';							
							}else{

								$arr_attr_keyword = array('style'=>'');
								$inp_del = '<input type=checkbox value="'.$Id.'" name="chk[]" id="chk_'.$Id.'">';
								$_dblist.='<td  style="text-align:center" width=50>'.$inp_del.'</td>';							
							}
							$_dblist.='<td  style="text-align:left">'.$_vgamecode.'</td>';	
							$_dblist.='<td  style="text-align:left"><a href="javascript:fnc_get_config('.$Id.',\''.$fnc->md5sum($Id.'@cfg').'\')">'.$_vconfigName.'</a></td>';	
							
							$_dblist.='<td  style="text-align:center">'.$fnc->date2vndate($_veffect_fromdate,false).' - '.$fnc->date2vndate($_veffect_todate,false).'</td>';	
							$_dblist.='<td  style="text-align:center">'.$fnc->date2vndate($_vfrom_date,false).' - '.$fnc->date2vndate($_vto_date,false).'</td>';							
							$_dblist.='<td  style="text-align:center" id="codedf_'.$Id.'">'.$_vqty_code_default.'</td>';	
							$_dblist.='<td  style="text-align:center">'.$_vmaxUser.'</td>';	
							$_dblist.='<td  style="text-align:center">'.$_vstatus_code.'</td>';	

																			
							
																												
					$_dblist.='</tr>';			
				
					
					$i++;
				}				
			
			
			if ($i==0){
					if ($this->TotalRecord==0)
						{
							$_dblist.='<tr><td colspan=100>Không tìm thấy thông tin</td></tr>';	
						}	
			}else{	}
			
				
		}
		catch (PDOException $ex)
		{}			
		
		$list[0] = $_dblist;
		$list[1] = $fnc->isNull($net_active,0);
		$list[2] = $fnc->isNull($net_inactive,0);
			
		return $list;			
		
	}	

	function pmtt_config_delete($statId, $epmttId_List){
		$sSQL="exec usp_pmtt_be_config_delete_v1 $statId, '".$epmttId_List."','".$_SESSION['ws_login_user']."'";	
		$this->db->query($sSQL, true, "Query failed");
	}


	function pmtt_config_add($fnc,$add_config_name,$add_gamecode_list,$add_from_date,$add_to_date,$add_status,$add_user,$add_level,$_pst_effect_fromdate,$_pst_effect_todate,$mode_pm,$mode_gamer,$mode_register,$_pst_kpi_pm,$_pst_kpi_gamer,$_pst_kpi_revenue,$_pst_kpi_playingtime)
	{
		if ($add_user=='')
			$add_user=200;
		if ($add_level=='')
			$add_level=0;


		if ($add_config_name!='' && $add_gamecode_list!='' && $add_from_date!='' && $add_to_date!=''  && $_pst_effect_fromdate!='' && $_pst_effect_todate!='' && $_pst_kpi_pm!='' && $_pst_kpi_gamer!='' && $_pst_kpi_revenue!='' && $_pst_kpi_playingtime!=''  )
		{
			$add_from_date = $fnc->vndate2date($add_from_date);
			$add_to_date = $fnc->vndate2date($add_to_date);
			$_pst_effect_fromdate = $fnc->vndate2date($_pst_effect_fromdate);
			$_pst_effect_todate = $fnc->vndate2date($_pst_effect_todate);



			$sSQL="exec usp_pmtt_be_config_add_v3 0,'".$add_config_name."','".$add_gamecode_list."','".$add_from_date."','".$add_to_date."',0,'".$_SESSION['ws_login_user']."',$add_user,$add_level,'".$_pst_effect_fromdate."','".$_pst_effect_todate."',$mode_pm,$mode_gamer,$mode_register,$_pst_kpi_pm,$_pst_kpi_gamer,$_pst_kpi_revenue,$_pst_kpi_playingtime";	
			//echo $sSQL;
			$result = $this->db->query($sSQL, true, "Query failed");	
			$aR = $this->db->fetchByAssoc($result);
			var_dump($aR);
			return $aR['result'];
		}
		return -1;
	
	}

	function files_upload($filename,$newfile){		
		$farr = explode('.',$filename['name']);
		$ext = $farr[count($farr)-1];
		$newfile = $newfile!='' ? $newfile.'.'.$ext : $filename['name'];
		$ftype = $this->getMime($filename);
		$fsize = $this->getSize($filename,5);
		if (($fsize) && strtolower($ext)=='csv'){
			$destination=$this->path_upload.'/'.$newfile;	
			if(!copy($filename['tmp_name'],$destination)) {
				$error[0]=1;
				$error[1]="ERROR: can't move_uploaded_file to $destination. You should try making the directory writable by the webserver";
				$error[2]=$destination;				
			}else{
			
				$error[0]=0;
				$error[1]=$newfile;
				$error[2]=$destination;
				$error[3]=$ftype;
				$error[4]=$fsize;				
				$error[5]=$ext;				
				chmod($destination,0777);
			}			
		}else
		{
			$error[0]=2;
			$error[1]='Invalid';			
			$error[2]=$destination;
			$error[3]=$ftype;
			$error[4]=$fsize;
			
		}


		return $error;
		
	}


	function getMime(&$_FILES_element)
	{

		$filename = $_FILES_element['name'];

		if( $_FILES_element['type'] )
		{
			$mime = $_FILES_element['type'];
		}
		elseif( function_exists( 'mime_content_type' ) )
		{
			$mime = mime_content_type( $_FILES_element['tmp_name'] );
		}
		elseif( function_exists( 'ext2mime' ) )
		{
			$mime = ext2mime( $_FILES_element['name'] );
		}
		else
		{
			$mime = ' application/octet-stream';
		}
		return strtoupper($mime);
	}
	
	function getSize(&$_FILES_element,$msize=1){
		$filename = $_FILES_element['size'];	
		$tsize =1 * 1024 * 1024;//1 Mb

		if (round($filename/$tsize,2)<=$msize)
			return true;
		else	
			return false;
	}

							  
								  




}


?>